<?php
	$_nav="info";
	$_title='资讯投稿';
	include_once 'head.php';

	if($user_id==''){
		header("Location:login.php");
		exit();
	}

	$infoc=new info();
	$userc=new user();

	$info_id=$_GET['id'];		//资讯id

	//资讯详情
	$key='*';
	$row=$infoc->infoDetail($info_id,$key);
	$smarty->assign('row',$row);

	//提交投稿申请
	if($_POST['apply']!=''){
		$thumbnail=$row['thumbnail'];
		if($_FILES['thumbnail']['name']!=''){
			//设置路径
			$folder = "./upload/info/";
			$annexFolder = $folder."large/";
			$smallFolder = $folder."small/";

			//删除旧图
			@unlink($annexFolder.$old_img);
			@unlink($smallFolder.$old_img);
		
			//上传新图
			$upimg = new uploadImg();
			$thumbnail = $upimg->upLoad('thumbnail',$annexFolder);
			//小图
			$upimg->smallImg($annexFolder.$thumbnail,$smallFolder);
		}

		$arr=array('title'=>$_POST['title'],'intro'=>$_POST['intro'],'type'=>'user',
				'thumbnail'=>$thumbnail,'user_id'=>$user_id,'class_id'=>$_POST['infoClass']
			);

		if($info_id==''){
			$arr['create_time']=time();
			$arr['check']='stay';				//待审核
			$id=$infoc->infoApplyAdd($arr);
			$info_id=$id;
			$msg='投稿成功！';
		}else{
			$res=$infoc->infoUpdate($arr,'id="'.$info_id.'"');
			$msg="修改成功！";
		}
		if($info_id>0){
			$arr_id=$_POST['label_id'];
			$res=$infoc->infoLabelAdd($arr_id,$info_id);
		}

		if($res){
			//echo $commonc->submitAlert($msg,$href);
		}
		//exit();
	}

	//咨询分类
	$info_class=$infoc->infoClassList();
	$smarty->assign('info_class',$info_class['result']);

	//文章绑定的标签
	$where=' AND il.info_id="'.$info_id.'"';
	$info_label=$infoc->infoLabel($where);

	//标签
	$where=" ORDER BY hot_num desc";
	$label=$infoc->infoLabelList($where,0);
	if(is_array($label['result'])){
		foreach($label['result'] as $k=>$v){
			if(is_array($info_label['result'])){
				foreach($info_label['result'] as $key=>$val){
					if($v['id']==$val['label_id']){
						$label['result'][$k]['lid']=$val['id'];
					}
				}
			}
		}
	}
	$smarty->assign('label',$label['result']);

	$smarty->display("info_apply.html");
?>